package org.workutil.buildmetadata.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.common.dto.ExcelDto;
import org.common.exception.MetadataException;
import org.common.util.ExcelUtil;
import org.common.util.XmlUtil;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.workutil.buildmetadata.dto.BizXmlDto;

public class BuildStdfieldUtil {

	private static String ENCODE = "UTF-8";

	private static ExcelDto excelDto = null;
	private static Map<String, String> businessMapped = null;

	public static void setEncode(String encode) {
		ENCODE = encode;
	}

	/**
	 * 生成stdfield文件
	 * 
	 * @param excelPath
	 *            excel文件路径
	 * @param sheetName
	 *            sheet名字
	 * @param businessTypePath
	 *            业务类型文件路径
	 * @param writePath
	 *            写入文件路径
	 * @throws Exception
	 */
	public static void wrtiteStdfield(String excelPath, String sheetName, String businessTypePath, String writePath)
			throws Exception {

		excelDto = ExcelUtil.initExceldto(excelPath, sheetName);
		initBusinessMapped(businessTypePath);

		File file = new File(writePath);
		OutputStream ou = null;
		try {
			file.createNewFile();
			ou = new FileOutputStream(file);
		} catch (IOException e) {
			e.printStackTrace();
		}

		Element root = new Element("standard-fields");
		Document Doc = new Document(root);

		for (int i = 0; i < excelDto.getDatas().size(); i++) {
			Element elements = new Element("standard-field");
			UUID uuid = UUID.randomUUID();
			String str = uuid.toString();
			elements.setAttribute("id", str.replaceAll("-", ""));
			elements.setAttribute("name", excelDto.getData(i, "字段名称").toString());
			elements.setAttribute("title", excelDto.getData(i, "字段中文名称").toString());

			String fieldType = excelDto.getData(i, "字段类型") == null ? ""
					: excelDto.getData(i, "字段类型").toString().toLowerCase();
			String fieldLength = excelDto.getData(i, "字段长度") == null ? "" : excelDto.getData(i, "字段长度").toString();
			String id = null;
			if (fieldLength.equals("")) {
				id = businessMapped.get(fieldType + (fieldType.equals("varchar") ? 32 : ""));
			} else {
				id = businessMapped.get(fieldType + fieldLength);
			}
			elements.setAttribute("business-type-id", id == null ? "" : id);

			elements.addContent(new Element("description").setText(""));
			elements.addContent(new Element("nick-names").setText(""));

			root.addContent(elements);
		}

		Format f = Format.getRawFormat();
		f.setEncoding(ENCODE);
		f.setIndent("  ");
		f.setOmitDeclaration(true);
		f.setOmitEncoding(true);
		XMLOutputter XMLOut = new XMLOutputter();
		XMLOut.setFormat(f);
		try {
			XMLOut.output(Doc, ou);
		} catch (IOException e) {
			throw new MetadataException("写入文件发生异常", e);
		}

	}

	/**
	 * 初始化业务类型name→id映射
	 * 
	 * @param businessTypePath
	 * @throws MetadataException
	 */
	private static void initBusinessMapped(String businessTypePath) throws MetadataException {
		businessMapped = new HashMap<String, String>();
		List<BizXmlDto> list = null;

		try {
			list = XmlUtil.createXmlDtoList(businessTypePath, BizXmlDto.class);
		} catch (Exception e1) {
			throw new MetadataException("创建xmldto发生异常", e1);
		}

		for (BizXmlDto dto : list) {
			businessMapped.put(dto.getName(), dto.getId());
		}

	}

}
